package com.example.music.service.Impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.music.dao.CommentMapper;
import com.example.music.dto.CommentDto;
import com.example.music.entity.Comment;
import com.example.music.service.CommentService;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CommentServiceImpl extends BaseServiceImpl<Comment, CommentMapper> implements CommentService {
    @Autowired
    CommentMapper commentMapper;

    @Override
    protected CommentMapper getMapper() {
        return commentMapper;
    }

    @Override
    public List<Comment> up(CommentDto commentDto) {
        UpdateWrapper<Comment> commentUpdateWrapper = new UpdateWrapper<>();

        Comment comment = new Comment();
        comment.setUserId(commentDto.getUserId());
        comment.setType(commentDto.getType());
        comment.setContent(commentDto.getContent());

        Map<String, Object> params = Maps.newHashMap();
        params.put("user_id",commentDto.getUserId());
        params.put("type",commentDto.getType());
        List<Comment> comments = selectItemsByParams(params);

        if (comments.size()>0){
            commentUpdateWrapper.set("up",0);
            update(comments.get(0),commentUpdateWrapper);
        }else {
            comment.setUp(1);
            add(comment);
        }
        return null;
    }
}















